<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
      <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
      <div class="filter-container">
        <el-button class="filter-item" type="primary" icon="el-icon-edit"
                   @click="handleAdd">新增
        </el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-upload"
                   @click="handleExport">批量上传服务商信息
        </el-button>
      </div>
      <!-- 查询结果 -->
      <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">

        <el-table-column align="center" min-width="100" label="服务商名称" prop="serviceName"/>
        <el-table-column align="center" min-width="140" label="服务商统一信用代码" prop="unicode"/>
        <el-table-column align="center" min-width="100" label="收款账号" prop="account"/>
        <el-table-column align="center" min-width="100" label="账号名称" prop="accountName"/>
        <el-table-column align="center" min-width="100" label="所属银行代码" prop="bankCode"/>
        <el-table-column align="center" min-width="100" label="开户联行号" prop="interBankNo"/>
        <el-table-column align="center" min-width="100" label="联系电话" prop="phone"/>
        <el-table-column align="center" min-width="100" label="手机号" prop="mobile"/>
        <el-table-column align="center" min-width="100" label="公司地址" prop="address"/>
        <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="handleEdit(scope.row)">修改
            </el-button>
            <!--<el-button type="danger" size="mini"-->
            <!--@click="doDelete(scope.row)">删除-->
            <!--</el-button>-->
            <el-popover
              placement="top"
              width="200"
              trigger="manual"
              :ref="'popover-' + scope.row.id">
              <p>确定删除该数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.id].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="doDelete(scope.row)">确定</el-button>
              </div>
              <el-button type="danger" size="mini" slot="reference" @click="$refs[`popover-` + scope.row.id].doShow()">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                  @pagination="getList"/>
      <el-dialog :visible.sync="editDialogVisible" :title="textMap[dialogStatus]" width="600px" :close-on-click-modal="false">
        <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="right"
                 label-width="150px" style="width: 400px; margin-left:20px;">
          <el-form-item label="服务商名称" prop="serviceName">
            <el-input v-model="editForm.serviceName"/>
          </el-form-item>
          <el-form-item label="服务商统一信用代码" prop="unicode">
            <el-input v-model="editForm.unicode" auto-complete="off" :disabled="this.bool"/>
          </el-form-item>
          <el-form-item label="收款账号" prop="account">
            <el-input v-model="editForm.account" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="账号名称" prop="accountName">
            <el-input v-model="editForm.accountName" auto-complete="off"/>
          </el-form-item>
          <!--<el-form-item label="银行信息" prop="selectedBankInfo">
            <el-cascader style="width:100%"
              :options="bankInfo"
              v-model="editForm.selectedBankInfo"
              @active-item-change="handleItemChange">
            </el-cascader>
          </el-form-item>-->
          <el-form-item label="所属银行代码" prop="bankCode">
            <el-select @change="bankNameChange" v-model="editForm.bankCode" placeholder="请选择"
                       filterable style="width:100%">
              <el-option
                v-for="item in banks"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.bankCode">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属银行名称" prop="bankName">
            <el-input readonly="readonly" v-model="editForm.bankName" auto-complete="off" placeholder="请选择" @click.native="getUnioLs()"/>
            <!--<el-select v-model="editForm.interBankNo" placeholder="请选择" filterable style="width:100%">
              <el-option
                v-for="item in unionnos"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.interBankNo"
              >
              </el-option>
            </el-select>-->
          </el-form-item>
          <!--<el-form-item label="所属银行代码" prop="bankCode">
            <el-input v-model="editForm.bankCode" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="开户联行号">
            <el-input v-model="editForm.interBankNo" auto-complete="off"/>
          </el-form-item>-->
          <el-form-item label="联系电话">
            <el-input v-model="editForm.phone" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="editForm.mobile" auto-complete="off"/>
          </el-form-item>
          <el-form-item label="公司地址">
            <el-input v-model="editForm.address" auto-complete="off"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <div slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取消</el-button>
            <el-button v-if="dialogStatus=='create'" type="primary" @click="doCreate">确定</el-button>
            <el-button v-else type="primary" @click="doEdit">确定</el-button>
          </div>
        </div>
      </el-dialog>
    <el-dialog :visible.sync="exportDialogVisible1" title="批量上传服务商信息" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 400px;">
        <!--<el-form-item label="请选择:" prop="bu">
          <el-radio-group v-model="editForm.bu">
            <el-radio :label="1">个人</el-radio>
            <el-radio :label="2">公司</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="开户银行名称" prop="m">
          <el-input v-model="editForm.m" auto-complete="off" placeholder="开户银行名称" style="width:250px;"/>
        </el-form-item>
      </el-form>
      <el-upload
        class="upload-demo"
        ref="upload"
        action="string"
        :before-upload="beforeUploadFile"
        :http-request="doUpload"
        :file-list="fileList"
        :limit="1"
        name="filename"
        :auto-upload="false">
        <el-button size="small" plain>选择文件</el-button>
        <div slot="tip" class="el-upload__tip">只能上传xlsx(Excel2007)文件，且不超过10M</div>
        <!--<el-button slot="trigger" size="small" type="primary">选取文件</el-button>-->
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button style="margin-left: 10px;" size="small" type="success" @click="exportDialogVisible1=false">取消
        </el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="$refs.upload.submit()">确定
        </el-button>
      </div>
    </el-dialog>

    <!--支行弹窗-->
    <el-dialog :visible.sync="UnionDialogVisible" title="选择开户银行名称" :close-on-click-modal="false">
      <div class="filter-container">
        <el-form :inline="true" status-icon label-position="left" class="demo-form-inline"
                 label-width="100px" style="width: 500px; margin-left:50px;">
          <el-form-item label="开户银行名称" prop="account">
            <el-input auto-complete="off" v-model="editForm1.interBankName" placeholder="开户银行名称" style="width:250px" />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" @click="queryUnio">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="unionnosLoading" :data="unionnos" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <el-table-column align="left" min-width="100" label="开户银行名称" prop="bankName"/>
        <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="choose(scope.row)">选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total1>0" :total="total1" :page.sync="editForm1.pageNum" :limit.sync="editForm1.pageSize"
                  @pagination="getUnionnos"/>
    </el-dialog>

    </div>
</template>

<script>
    import {doGet, doPost, doFile} from '@/api'
    import Pagination from '@/components/Pagination'

    const FETCH_PAGE = 'serviceman.html'
    const FETCH_URL = {
      LIST: `${FETCH_PAGE}?op=list`,
      CREATE: `${FETCH_PAGE}?op=add`,
      EDIT: `${FETCH_PAGE}?op=batchadd`,
      DELETE: `${FETCH_PAGE}?op=chg`,
      IMPORT: `${FETCH_PAGE}?op=del`
    }
  export default {
    name: 'syslog',
      al : '',
      pageNum: '',
      pageSize: '',
    components: { Pagination },
    data() {
      return {
        bool:false,
        list: [],
        total: 0,
          total1: 0,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
        },
        dialogStatus: undefined,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        editForm: {
          serviceName: undefined,
          unicode: undefined,
          account: undefined,
          accountName: undefined,
          bankCode: undefined,
          interBankNo: undefined,
          phone: undefined,
          mobile: undefined,
          address: undefined,
          selectedBankInfo: undefined,
            bankName: undefined,
          bu: 1,
          m: undefined
        },
        editFormRules: {
          serviceName: [{required: true, message: '服务商名称不允许为空', trigger: 'blur'}],
          unicode: [{required: true, message: '服务商统一信用代码不允许为空', trigger: 'blur'}],
          account: [{required: true, message: '收款账号不允许为空', trigger: 'blur'}],
          accountName: [{required: true, message: '账号名称不允许为空', trigger: 'blur'}],
          bankCode: [{required: true, message: '所属银行代码不允许为空', trigger: 'blur'}],
          selectedBankInfo: [{required: true, message: '请选择银行信息', trigger: 'blur'}],
          bankName: [{required: true, message: '所属银行名称不允许为空', trigger: 'blur'}],
        },
        editDialogVisible: false,
        exportDialogVisible1: false,
        editIndex: undefined,
        fileList: [],
        bankInfo: [],
        banks: [],
        unionnos: [],
          editForm1: {
              pageNum: 1,
              pageSize: 20,
              bankCode: undefined,
              interBankName: undefined
          },
          UnionDialogVisible: false,
          unionnosLoading: true,
      }
    },
    created() {
      this.getList()
        this.getBanks()
    },
    methods:{
        getBanks() {
            doGet('bank.html?op=list', {}).then(response => {
                this.banks = response.rows
            }).catch(() => {
                this.banks = []
            })
        },
        getUnionnos(subankCode, flag) {
            if (typeof(subankCode) == 'string') {
                this.pageNum = 1
                this.pageSize = 20
            } else {
                this.pageNum = subankCode.page
                this.pageSize = subankCode.limit
            }
            this.editForm1 = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                bankCode: this.al,
                bankName: this.editForm1.interBankName,
                interBankNo: this.editForm.interBankNo,
                flag: flag
                //bankName: this.editForm.interBankNo
            }
            this.UnionDialogVisible = true
            this.unionnosLoading = true
            doGet('unionno.html?op=list', this.editForm1).then(response => {
                this.unionnos = response.rows
                this.total1 = response.data.totalcount
                this.unionnosLoading = false
            }).catch(() => {
                this.unionnos = []
                this.total1 = 0
                this.unionnosLoading = false
            })
        },
        bankNameChange(bankCode) {
            this.al = bankCode.substring(0, 3)
            var flag = '1';
            this.getUnionnos(this.al, flag)
            this.editForm.interBankNo = undefined
        },
      getList() {
        this.listLoading = true;
        doGet(FETCH_URL.LIST, this.listQuery).then(response => {
//          console.log(response)
          this.list = response.rows;
          this.total = response.data.totalcount;
          this.listLoading = false
        }).catch(() => {
          this.list = [];
          this.total = 0
          this.listLoading = false
        })
      },
      handleEdit(row) {
        this.editForm = Object.assign({}, row)
        this.editIndex = this.list.indexOf(row)
        this.dialogStatus = 'update'
        this.editDialogVisible = true
        this.bool=true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
        this.editForm.selectedBankInfo = [row.bankCode, row.interBankNo]
        if (!this.bankInfo.length) {
          this.getBankCodeInfo()
        } else {
          let bankArr = this.bankInfo.filter(item => {
            return row.bankCode == item.value
          })[0]
          if (bankArr && !bankArr.children.length) {
            doGet('unionno.html?op=list', {bankName: bankArr.label}).then(response => {
              this.bankInfo.forEach(item => {
                if (item.value == bankArr.value) {
                  item.children = response.rows.map(items => {
                    return {
                      value: items.interBankNo,
                      label: items.bankName
                    }
                  })
                }
              })
            }).catch(() => {
              // this.bankInfo = []
            })
          }
        }
      },
      handleAdd() {
        if (!this.bankInfo.length) {
          this.getBankCodeInfo()
        }
        this.editForm.serviceName = undefined;
        this.editForm.unicode = undefined;
        this.editForm.account = undefined;
        this.editForm.accountName = undefined;
        this.editForm.bankCode = undefined;
        this.editForm.interBankNo = undefined;
        this.editForm.phone = undefined;
        this.editForm.mobile = undefined;
        this.editForm.address = undefined;
        this.editDialogVisible = true;
        this.editForm.selectedBankInfo = undefined
        this.bool=false
        this.dialogStatus = 'create'
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },


      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm,
              bankCode: this.editForm.selectedBankInfo[0],
              interBankNo: this.editForm.selectedBankInfo[1],
              selectedBankInfo: undefined
            }
            doGet(FETCH_URL.DELETE, sendData)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.list[this.editIndex] = this.editForm
                this.$notify.success({
                  title: '成功',
                  message: '修改成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      doCreate() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            let sendData = {
              ...this.editForm,
              /*bankCode: this.editForm.selectedBankInfo[0],
              interBankNo: this.editForm.selectedBankInfo[1],*/
                bankCode: this.editForm.bankCode,
                interBankNo: this.editForm.interBankNo,
              selectedBankInfo: undefined
            };
            doGet(FETCH_URL.CREATE, sendData)
              .then(response => {
                this.getList();
                this.editDialogVisible = false;
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      doDelete(row) {
        doGet(FETCH_URL.IMPORT, {unicode: row.unicode})
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
            this.total--
            this.$refs[`popover-` + row.id].doClose()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      },
      handleExport(value) {
        this.exportDialogVisible1 = true
      },
      beforeUploadFile(file) {
        let extension = file.name.substring(file.name.lastIndexOf('.')+1)
        let size = file.size / 1024 / 1024
        if(extension !== 'xlsx'&&extension !== 'xls') {
          this.$notify.warning({
            title: '警告',
            message: `只能上传Excel（即后缀是.xlsx或.xls）的文件`
          });
          return false
        }
        // if(size > 10) {
        //   this.$notify.warning({
        //     title: '警告',
        //     message: `文件大小不得超过10M`
        //   });
        // }
      },
      doUpload(param) {
        console.log('upload==>')
        let fd = new FormData()
        fd.append('filename', param.file)//传文件\
        doFile('serviceman.html?op=batchadd', fd).then((res)=> {
          this.getList()
          this.exportDialogVisible1 = false
          this.$notify.success({
            title: '成功',
            message: '上传成功'
          })
        }).catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.message
          })
        })
      },

      // 获取银行代码信息
      getBankCodeInfo() {
        doGet('bank.html?op=list').then(response => {
          this.bankInfo = response.rows.map(item => {
            return {
              value: item.bankCode,
              label: item.bankName,
              children: []
            }
          })
          if (this.dialogStatus == "update") { // 修改信息执行
            let bankArr = this.bankInfo.filter(item => {
              return this.editForm.bankCode == item.value
            })[0]
            if (bankArr && !bankArr.children.length) {
              doGet('unionno.html?op=list', {bankName: bankArr.label}).then(response => {
                this.bankInfo.forEach(item => {
                  if (item.value == bankArr.value) {
                    item.children = response.rows.map(items => {
                      return {
                        value: items.interBankNo,
                        label: items.bankName
                      }
                    })
                  }
                })
              }).catch(() => {
                // this.bankInfo = []
              })
            }
          }
        }).catch(() => {
          this.bankInfo = []
        })
      },

      // 银行代码第一级选择触发
      handleItemChange(val) {
        let bankArr = this.bankInfo.filter(item => {
          return item.value == val
        })[0]
        if (!bankArr.children.length) {
          doGet('unionno.html?op=list', { bankName: bankArr.label }).then(response => {
            this.bankInfo.forEach(item => {
              if (item.value == val) {
                item.children = response.rows.map(items => {
                  return {
                    value: items.interBankNo,
                    label: items.bankName
                  }
                })
              }
            })
          }).catch(() => {
            // this.bankInfo = []
          })
        }
      },
      getUnioLs() {
        if(this.al == null){
            alert('请选择银行代码')
          } else {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '1')
          }
      },
        /**查询银行名称*/
        queryUnio() {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '2')
        },
        /** 选择分行*/
        choose(row) {
            this.editForm.interBankNo = row.interBankNo
            this.editForm.bankName = row.bankName
            this.editForm1.interBankName = row.bankName
            this.UnionDialogVisible = false
        },
    }
  }
</script>
